package com.hngy.xpq.android_practice.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.hngy.xpq.android_practice.bean.Recording;
import com.hngy.xpq.android_practice.utils.CommOrder;
import com.hngy.xpq.android_practice.utils.RecordingOrder;

import java.util.ArrayList;
import java.util.List;

public class SQLiteRecording extends SQLiteOpenHelper {     //<浏览历史>
    SQLiteDatabase sqLiteDatabase;
    public SQLiteRecording( Context context) {
        super(context, RecordingOrder.DATABASE_DATA, null, RecordingOrder.DATABASE_INS);
        sqLiteDatabase = this.getReadableDatabase();
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + RecordingOrder.DATABASE_TABLE + "("
                + RecordingOrder.NOTEPAD_ID + " integer primary key autoincrement,"
                + RecordingOrder.NOTEPAD_NAME + " VARCHAR(30),"
                + RecordingOrder.NOTEPAD_MER_NAME + " VARCHAR(30),"
                + RecordingOrder.NOTEPAD_USER_NAME + " VARCHAR(30),"
                + RecordingOrder.NOTEPAD_LIKE + " VARCHAR(20),"
                + RecordingOrder.NOTEPAD_TIME + " VARCHAR(30))");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    public boolean recording_add(Recording r){
        ContentValues values = new ContentValues();
        values.put(RecordingOrder.NOTEPAD_NAME,r.getRecording_name());
        values.put(RecordingOrder.NOTEPAD_TIME,RecordingOrder.getTimeDate());
        values.put(RecordingOrder.NOTEPAD_USER_NAME,r.getUser_name());
        values.put(RecordingOrder.NOTEPAD_MER_NAME,r.getMerchant_name());
        values.put(RecordingOrder.NOTEPAD_LIKE,r.getLikes());
        return
                sqLiteDatabase.insert(RecordingOrder.DATABASE_TABLE,null,values)>0;
    }

    public List<Recording> recordingQuery(String name){
        List<Recording> list = new ArrayList<Recording>();
        try{
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + RecordingOrder.DATABASE_TABLE + " where " + RecordingOrder.NOTEPAD_USER_NAME + " = ?", new String[]{ name }, null);
            if (cursor != null){
                while (cursor.moveToNext()){
                    Recording r = new Recording();
                    r.setId(cursor.getInt(cursor.getColumnIndex(RecordingOrder.NOTEPAD_ID)));
                    r.setRecording_name(cursor.getString(cursor.getColumnIndex(RecordingOrder.NOTEPAD_NAME)));
                    r.setLikes(cursor.getString(cursor.getColumnIndex(RecordingOrder.NOTEPAD_LIKE)));
                    list.add(r);
                }
                cursor.close();
            } else {
                System.out.println("浏览历史出现问题!!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    public boolean updateLike(int id,String like){
        String sql = RecordingOrder.NOTEPAD_ID + "=?";
        String[] strings = new String[]{String.valueOf(id)};
        ContentValues values = new ContentValues();
        values.put(RecordingOrder.NOTEPAD_LIKE,like);
        return
                sqLiteDatabase.update(RecordingOrder.DATABASE_TABLE,values,sql,strings)>0;
    }

    public int queryIdAndName(String recording_name) {
        int a = 0;
        Cursor x = sqLiteDatabase.rawQuery("select * from " + RecordingOrder.DATABASE_TABLE + " where " + RecordingOrder.NOTEPAD_NAME + " = ?", new String[]{ recording_name }, null);
        if (x != null) {
            while (x.moveToNext()) {
                a = x.getInt(x.getColumnIndex(RecordingOrder.NOTEPAD_ID));
            }
            x.close();
        }
        return a;
    }

    public List<Recording> recordingLikeQuery(String like){
        List<Recording> list = new ArrayList<Recording>();
        try{
            Cursor cursor = sqLiteDatabase.rawQuery("select * from " + RecordingOrder.DATABASE_TABLE + " where " + RecordingOrder.NOTEPAD_LIKE + " = ?", new String[]{ like }, null);
            if (cursor != null){
                while (cursor.moveToNext()){
                    Recording r = new Recording();
                    r.setId(cursor.getInt(cursor.getColumnIndex(RecordingOrder.NOTEPAD_ID)));
                    r.setRecording_name(cursor.getString(cursor.getColumnIndex(RecordingOrder.NOTEPAD_NAME)));
                    r.setLikes(cursor.getString(cursor.getColumnIndex(RecordingOrder.NOTEPAD_LIKE)));
                    list.add(r);
                }
                cursor.close();
            } else {
                System.out.println("浏览历史出现问题!!");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
